package com.dw.dwdzcommerce.view.mainContent.list;

import com.dw.dwdzcommerce.R;
import com.dw.dwdzcommerce.R.layout;
import com.dw.dwdzcommerce.entity.ShopImage;
import com.dw.dwdzcommerce.tools.commons.bitmap.BitmapUtil;
import com.dw.dwdzcommerce.tools.commons.viewUtil.ViewUtil;
import com.dw.dwdzcommerce.view.mainContent.mainActionBar.PopupType;
import com.dw.dwdzcommerce.view.mainContent.mainActionBar.PopupType.OnItemClickListener;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainContentListLayout {

	private View mRoot;
	private TextView mShopNameTv;
	private TextView mShopLocTv;
	private TextView mShopRuleTv;
	private TextView mShopTimeTv;
	private TextView mShopPhoneTv;
	private TextView mShopMoreTv;

	private LinearLayout mShopContent;
	private ImageView mShopAvatorImg;
	private ImageView mShopFocusImg;
	private ImageView mShopLocImg;
	private FragmentActivity mContext;
	private int mShopId;

	public void initModule(View root, FragmentActivity mContext) {
		mRoot = root;
		this.mContext = mContext;
		initView();
	}

	private void initView() {
		mShopNameTv = (TextView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopName_tv);
		mShopLocTv = (TextView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopLoc_tv);
		mShopRuleTv = (TextView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shoprule_tv);
		mShopTimeTv = (TextView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopTime_tv);
		mShopPhoneTv = (TextView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopPhone_tv);
		mShopMoreTv = (TextView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopruleMore_tv);
		mShopAvatorImg = (ImageView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopavator_img);
		mShopFocusImg = (ImageView) mRoot.findViewById(R.id.item_fragment_main_content_lis_focus_img);
		mShopLocImg = (ImageView) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopLoc_img);
		mShopFocusImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				PopupType p = new PopupType();
				p.init(mContext);
				p.setData(new String[] { "关注", "提醒" });
				p.setmOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(View view, int pos, int arg0) {
						if (mListener != null) {
							mListener.onFocus(view, mShopId);
						}
					}
				});
				p.show(v);
			}
		});
		mShopLocImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mListener != null) {
					mListener.onLocClick(v, mShopId);
				}
			}
		});
		setUpMore();

	}

	private void setUpMore() {
		// mShopMoreTv.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// mShopMoreTv.setVisibility(View.GONE);
		// mShopRuleTv.setMaxLines(10000);
		// }
		// });
		
		mShopMoreTv.post(new Runnable() {

			@Override
			public void run() {
				int cut = ViewUtil.getScreenWidth(mContext) * 2 / 3;
				// int cut = ViewUtil.getScreenWidth(mContext) -
				// mShopMoreTv.getMeasuredWidth()
				// - mShopMoreTv.getPaddingLeft() -
				// mShopMoreTv.getPaddingRight() - mShopRuleTv.getPaddingLeft()
				// -
				// mShopRuleTv.getPaddingRight()-mRoot.getPaddingLeft()-mRoot.getPaddingRight();
				int measuredWidth = mShopRuleTv.getMeasuredWidth();
				if (measuredWidth >= cut) {
					// 可显示
					mShopMoreTv.setVisibility(View.VISIBLE);
					mShopMoreTv.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							mShopRuleTv.setMaxLines(10000);
							mShopMoreTv.setVisibility(View.GONE);
						}
					});
				} else {
					mShopMoreTv.setVisibility(View.GONE);
				}

			}
		});

	}

	public int getmShopId() {
		return mShopId;
	}

	public void setmShopId(int mShopId) {
		this.mShopId = mShopId;
	}

	public View getView() {
		return mRoot;
	}

	public void setShopName(String name) {
		mShopNameTv.setText("" + name);
	}

	public void setShopLoc(String loc) {
		mShopLocTv.setText("" + loc);
	}

	public void setShopRule(String rule) {
		mShopRuleTv.setText("" + rule);
	}

	public void setShopTime(String time) {
		mShopTimeTv.setText("" + time);
	}

	public void setShopContent(ShopImage[] shopImages) {
		mShopContent = (LinearLayout) mRoot.findViewById(R.id.item_fragment_main_content_lis_shopContent_vp);
		ViewPager vp = new ViewPager(mContext);
		vp.setId(ViewUtil.generateViewId());
		vp.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		ViewUtil.configView(mShopContent, mContext);
		// vp
		ShopImageViewPagerAdapter adapter = new ShopImageViewPagerAdapter(mContext.getSupportFragmentManager(),
				shopImages);
		vp.setAdapter(adapter);
		mShopContent.addView(vp);

	}

	public void setShopAvator(String imageUrl, Context context) {
		BitmapUtil.loadImage(mShopAvatorImg, imageUrl, context);
	}

	class ShopImageViewPagerAdapter extends FragmentPagerAdapter {
		Fragment[] mFragments;

		public ShopImageViewPagerAdapter(FragmentManager fm, ShopImage[] mData) {
			super(fm);
			mFragments = ShopImageFragment.mkFragment(mData);
		}

		public ShopImageViewPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return mFragments[arg0];
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mFragments.length;
		}

	}

	private OnContentClickListener mListener;

	public void setmListener(OnContentClickListener mListener) {
		this.mListener = mListener;
	}

	public interface OnContentClickListener {
		void onFocus(View view, int mShopId);

		void onLocClick(View view, int mShopId);
	}

	public void setPhone(String phone) {
		mShopPhoneTv.setText("" + phone);

	}

}
